﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace YJKDataLookAfter
{
	public partial class DlgUnregisterUser : System.Windows.Forms.Form
	{
		public DlgUnregisterUser()
		{
			this.InitializeComponent();
		}

		private void RemoveUser_Load(object sender, EventArgs e)
		{
			foreach (DbUser item in DbUser.GetAllUsers(BimServerDB.Default))
			{
				this.cbxUsers.Items.Add(item);
			}
			if (this.cbxUsers.Items.Count > 0)
			{
				this.cbxUsers.SelectedIndex = 0;
			}
		}

		private void btnRemove_Click(object sender, EventArgs e)
		{
			if (this.cbxUsers.SelectedItem != null)
			{
				try
				{
					DbUser dbUser = (DbUser)this.cbxUsers.SelectedItem;
					if (dbUser.Name == "admin")
					{
						MessageBox.Show("不能注销系统管理员账户！");
					}
					else
					{
						dbUser.Unregister();
						this.cbxUsers.Items.RemoveAt(this.cbxUsers.SelectedIndex);
						if (this.cbxUsers.Items.Count > 0)
						{
							this.cbxUsers.SelectedIndex = 0;
						}
						MessageBox.Show(dbUser.ToString() + "账户已注销。");
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message);
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}
	}
}
